/**
 * 文件下载
 * url: 下载文件的api地址
 * headers: 请求头配置
 */
export function $fileDownload(url, name, headers, callback) {
	return new Promise((resolve, reject) => {
		fetch(url, { method: 'GET', cache: 'no-cache', headers }).then(res => {
			const readableStream = res.body;
			const fileStream = streamSaver.createWriteStream(name, { size: res.headers.get('content-length') });
			if (!!callback && typeof callback === 'function') {
				callback();
			}
			if (window.WritableStream && readableStream.pipeTo) {
				return readableStream.pipeTo(fileStream).then(() => console.log('done writing'));
			}
			window.writer = fileStream.getWriter();
			const reader = readableStream.getReader();
			const pump = () => reader.read().then((res) => {
				return res.done ? window.writer.close() : window.writer.write(res.value).then(pump)
			});
			pump();
		})
	})
}